/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jbr.simplefoodplan.entities;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author jbr
 */
//@PersistenceCapable
@XmlRootElement(name = "plan")
@XmlAccessorType(XmlAccessType.NONE)
public class Plan implements SecurableEntity {

    private Entity entity;
    private transient boolean addDishToPlan;

    public Plan() {
        this.entity = new Entity("plan");
    }

    public Plan(Entity entity) {
        this.entity = entity;
    }

    @Override
    public Entity getEntity() {
        return this.entity;
    }

    @XmlElement
    public Date getDate() {
        return (Date)entity.getProperty("date");
    }

    public void setDate(Date date) {
        System.out.println("Set date is  called to: " + date);
        entity.setProperty("date", date);
    }

    @XmlElement
    public Long getId() {
        return entity.getKey().getId();
    }

    public void setId(Long id) {
        System.out.println("Setting plan id to " + id);
        if (id == null)
            return;
        try {
            Entity oldEntity = entity;
            entity = DatastoreServiceFactory.getDatastoreService()
                    .get(KeyFactory.createKey("plan", id));
            setDishId((Long) oldEntity.getProperty("dishId"));
            setDate((Date) oldEntity.getProperty("date"));
            System.out.println("Entity found!");
        } catch (EntityNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public User getUser() {
        return (User)entity.getProperty("user");
    }

    @Override
    public void setUser(User user) {
        entity.setProperty("user", user);
    }

    @XmlElement
    public Long getDishId() {
        return (Long)entity.getProperty("dishId");
    }

    public void setDishId(Long dishId) {
        System.out.println("SetDishId is called id=" + dishId);
        entity.setProperty("dishId", dishId);
    }

    public boolean isAddDishToPlan() {
        return addDishToPlan;
    }

    public void setAddDishToPlan(boolean addDishToPlan) {
        this.addDishToPlan = addDishToPlan;
    }

    @XmlElement
    public Dish getDish() {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        if (getDishId() != null && addDishToPlan) {
            try {
                final Dish dish = new Dish(datastore.get(KeyFactory.createKey("dish", getDishId())));
                return dish;
            } catch (EntityNotFoundException e) {
                return null;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "Plan{" + "id=" + getId() + " user=" + getUser() + " date=" + getDate() + " dishId=" + getDishId() + " addDishToPlan=" + addDishToPlan + '}';
    }




    

}
